<template>
  <h1>当前求和：{{ sum }}</h1>
  <h2>姓名：{{ person.name }}</h2>
  <h2>年龄：{{ person.age }}</h2>
  <h2>工资：{{ person.job.j1.salary }}</h2>

  <button @click="sum ++">+1</button>
  <button @click="person.name += '~'">改姓名</button>
  <button @click="person.age++">改年龄</button>
  <button @click="person.job.j1.salary++">涨工资</button>
</template>
  
  <script>
import { reactive, computed, ref, watch } from "vue";
export default {
  name: "App",
  components: {},

  setup(props, context) {
    let sum=ref(0)
    let person = ref({
      name: "张三",
      age: 18,
      job: {
        j1: {
          salary: 20,
        },
      },
    });
// 不能监听普通的值  要是 ref 或者reactive包裹后的值
    // reactive响应式数据  无法正确获取新旧值
    // 强制开启深度监视
    // watch(
    //   person,
    //   (newValue, oldValue) => {
    //     console.log(newValue, oldValue);
    //   },
    //   {
    //     immediate: true,
    //     deep: true,
    //   }
    // );
    // 监视的是proxy  或者开启深度监视   
    watch(
      person.value,
      (newValue, oldValue) => {
        console.log(newValue, oldValue);
      },
      {
        immediate: true,
        deep: true,
      }
    );
    watch(
      sum,
      (newValue, oldValue) => {
        console.log(newValue, oldValue);
      },
      {
        immediate: true,
        deep: true,
      }
    );
   

    return { person ,sum};
  },
};
</script>
  
  <style>
</style>
  